package dao.impl;

import dao.UsersDao;
import entity.Users;
import util.BaseDao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UsersDaoImpl extends BaseDao implements UsersDao {
    @Override
    public List<Users> query() {
        List<Users> list = new ArrayList<Users>() ;
        Connection connection = super.getConn() ;
        String sql = "select * from users" ;
        Statement stmt = null ;
        ResultSet resultSet = null ;
        try {
            stmt = connection.createStatement() ;
            resultSet = stmt.executeQuery(sql) ;
            while(resultSet.next()){
                list.add(new Users(resultSet.getInt("id"),resultSet.getString("name"),resultSet.getString("address"),resultSet.getString("gender"),resultSet.getString("email"))) ;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(resultSet,stmt,connection);
        }
        return list;
    }

    @Override
    public int del(int id) {
            int i = 0 ;
            Connection connection = super.getConn() ;
            String sql = "delete from users where id=?" ;
            PreparedStatement stmt = null ;
            try {
                stmt = connection.prepareStatement(sql) ;
                stmt.setInt(1,id);
                i = stmt.executeUpdate() ;
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                super.closeAll(null,stmt,connection);
            }
            return i;
    }

    @Override
    public int add(Users users) {
        int i = 0 ;
        Connection connection = super.getConn() ;
        String sql = "insert into users (name,address,email,gender) values (?,?,?,?)" ;
        PreparedStatement stmt = null ;
        try {
            stmt = connection.prepareStatement(sql) ;

            stmt.setString(1,users.getName());
            stmt.setString(2,users.getAddress());
            stmt.setString(3,users.getEmail());
            stmt.setString(4,users.getGender());
            i = stmt.executeUpdate() ;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(null,stmt,connection);
        }
        return i;
    }

    @Override
    public int update(Users users) {
        int i = 0 ;
        Connection connection = super.getConn() ;
        String sql = "update users set name =?,address=?,email=?,gender=? where id=?" ;
        PreparedStatement stmt = null ;
        try {
            stmt = connection.prepareStatement(sql) ;
            stmt.setString(1,users.getName());
            stmt.setString(2,users.getAddress());
            stmt.setString(3,users.getEmail());
            stmt.setString(4,users.getGender());
            stmt.setInt(5,users.getId());
            i = stmt.executeUpdate() ;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(null,stmt,connection);
        }
        return i;
    }

    @Override
    public Users getUsers(int id) {
        Users users = null ;
        Connection connection = super.getConn() ;
        String sql = "select * from users where id=?" ;
        PreparedStatement stmt = null ;
        ResultSet resultSet = null ;
        try {
            stmt = connection.prepareStatement(sql) ;
            stmt.setInt(1,id);
            resultSet = stmt.executeQuery() ;
            if (resultSet.next()){
                users = new Users(resultSet.getInt("id"),resultSet.getString("name"),resultSet.getString("address"),resultSet.getString("gender"),resultSet.getString("email")) ;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(resultSet,stmt,connection);
        }
        return users;
    }
}
